package com.wandousoushu.jiusen.ui.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wandousoushu.jiusen.MainActivity;
import com.wandousoushu.jiusen.MyApplication;
import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.web.http.MainHttpUtils;

import org.json.JSONObject;

public class FeedbackActivity extends AppCompatActivity {

    private TextView commit;
    private EditText feedbackEditText;
    private LinearLayout backLinearLayout;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        commit=findViewById(R.id.feedback_tv_commit);
        feedbackEditText=findViewById(R.id.feedback_edittext);
        backLinearLayout=findViewById(R.id.feedback_ll_back);
        backLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                content=feedbackEditText.getText().toString().trim();
                if(TextUtils.isEmpty(content)){
                    mHandle.sendEmptyMessage(3);
                }else {
                    HttpCommit();
                }
            }
        });
    }

    private void HttpCommit(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String returnData = MainHttpUtils.getInstance().FeedBackCommit(content, MyApplication.getInstance().getToken());
                    if (returnData == null) {
                        mHandle.sendEmptyMessage(0);
                        return;
                    }
                    JSONObject jsonObject=new JSONObject(returnData);
                    if(jsonObject!=null&&jsonObject.getInt("code")==1){
                        mHandle.sendEmptyMessage(1);
                    }else {
                        Message msg = Message.obtain();
                        msg.obj = jsonObject.getString("msg");
                        msg.what = 2;
                        mHandle.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler mHandle=new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.feedback_commit_fail), Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.feedback_commit_success), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.feedback_hint), Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}